/**
 * 
 */
package org.ranch.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;

/**
 * Represents a Kraal
 *
 */
@Entity
@Table(name = "kraal")
public class Kraal extends BaseData {

    /** kraal name */
    private String name;
    /** Farm the kraal belongs to */
    private Farm farm;
    /** Animals belonging to this kraal */
    private List<Animal> animals;

    /** Default constructor */
    public Kraal() {
        super();
    }

    public Kraal(String name, Farm farm, List<Animal> animals) {
        super();
        setName(name);
        setFarm(farm);
        setAnimals(animals);
    }
    
    @SuppressWarnings("deprecation")
    @OneToMany(mappedBy = "kraal", cascade = { CascadeType.ALL })
    @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE,
	    org.hibernate.annotations.CascadeType.DELETE,
	    org.hibernate.annotations.CascadeType.MERGE,
	    org.hibernate.annotations.CascadeType.PERSIST,
	    org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    public List<Animal> getAnimals() {
        return animals;
    }

    public void setAnimals(List<Animal> animals) {
        this.animals = animals;
    }

    @ManyToOne
    @JoinColumn(name = "farm_id", nullable = true)
    public Farm getFarm() {
        return farm;
    }

    public void setFarm(Farm farm) {
        this.farm = farm;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * adds a given animal to a kraal
     *
     * @param animal
     */
    public void addAnimal(Animal animal) {
	if (animal == null) {
	    return;
	}

	if (getAnimals() == null) {
	    setAnimals(new ArrayList<Animal>());
	}

	getAnimals().add(animal);
	animal.setKraal(this);
    }

    /**
     * removes a given animal from the kraal
     *
     * @param animal
     */
    public void removeAnimal(Animal animal) {
	if (animal == null || getAnimals() == null) {
	    return;
	}

	getAnimals().remove(animal);
    }


}
